package com.cn.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * &#064;Time 2024 八月 星期五 22:14
 *
 * @author ShangGuan
 */
public class MapUtils {

    /**
     * 映射到FORM表单里面的数据数据
     *
     * @param map      地图
     * @param boundary 边界
     * @return {@link String }
     */
    public static String mapToFromData(Map<String, Object> map, String boundary) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            stringBuilder.append(boundary).append("\r\n");
            stringBuilder.append("Content-Disposition:form-data; name=\"")
                    .append(key).append("\"").append("\r\n");
            stringBuilder.append(value).append("\r\n");
        }
        return stringBuilder.toString();
    }

    public static String mapToFormUrlEncoded(Map<String, Object> map) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            stringBuilder.append(key).append("=").append(value).append("&");
        }
        return stringBuilder.toString().endsWith("&") ?
                stringBuilder.substring(0, stringBuilder.toString().length() - 1) : stringBuilder.toString();
    }

    // 将Map<String, Object>转换为URL查询参数字符串
    public static String mapToUrlParams(Map<String, Object> map) throws UnsupportedEncodingException {
        StringBuilder urlParams = new StringBuilder();

        int entryCount = 0;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = URLEncoder.encode(entry.getKey(), "UTF-8");
            String value = URLEncoder.encode(entry.getValue().toString(), "UTF-8");
            urlParams.append(key)
                    .append("=")
                    .append(URLEncoder.encode(value, StandardCharsets.UTF_8.name()));
            if (entryCount < map.size() - 1) {
                urlParams.append("&");
            }
            entryCount++;
        }

        return urlParams.toString();
    }

    public static boolean isEmpty(Map<Object, Object> map) {
        return map == null || map.isEmpty();
    }


    public static String mapStrToJson(Map<String, Object> map) {
        return mapToJson(new HashMap<>(map));
    }

    // 方法将Map<String, Object>转换为JSON字符串
    public static String mapToJson(Map<Object, Object> map) {
        StringBuilder json = new StringBuilder();
        json.append("{");
        int entryCount = 0;
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            json.append("\"").append(entry.getKey()).append("\":");
            Object value = entry.getValue();
            CollectionUtils.checkObjectType(value, json);
            if (entryCount < map.size() - 1) {
                json.append(",");
            }
            entryCount++;
        }
        json.append("}");
        return json.toString();
    }


}
